<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/4/21
 * Time: 10:00
 */
namespace Admin\Controller;
class ModelController extends AdminBase{


    public function index(){

        $list=$this->Pages('Model');
        $this->assign('lists',$list);
        $this->display();
    }
    /*模型禁用*/
    public function disabled($mid,$dis){
        if(!AJAX || !$mid){return false;}

        switch($dis){
            case 0:
                $data['disabled']=1;
                break;
            case 1:
                $data['disabled']=0;
                break;

        }
        $where['modelid']=$mid;
        $res=M('model')->where($where)->save($data);
        $this->ajaxReturn(validreturn($res,'操作成功！','操作失败！'));


    }
    /*模型删除*/
    public function model_del($mid){
        if(ISAJAX){
            $this->ajaxreturn(validreturn(D('Model')->del_model($mid)));
         }

    }
    /*创建模型*/
    public function add_model($mid=''){

        if(IS_POST){
            $tablename=I('post.tablename');

            if(!$mid){
                if(!$tablename){$this->ThisReturn(false);}
                $this->ThisReturn(D('Model')->Model_add($tablename));

            }else{
                $model=M('model')->where(array('modelid'=>$mid))->find();
                if($model){
                    M()->execute("ALTER TABLE ".C('DB_PREFIX').$model['tablename']." rename  ".C('DB_PREFIX').$tablename);
                    M()->execute("ALTER TABLE ".C('DB_PREFIX').$model['tablename']."_data rename  ".C('DB_PREFIX').$tablename.'_data');
                    $this->ThisReturn(DB_edit('model','','','save',array('modelid'=>$mid)));
                }else{
                    $this->ThisReturn(false);
                }


            }




        }
        if($mid){
            $where['modelid']=$mid;
            $info=M('model')->where($where)->find();
            $this->assign($info);
        }

        $this->display();
    }
    /*创建模型时检查字段重复*/
    public function check_model($id=''){
        if(ISAJAX){
            if($id){$where['modelid']=array('neq',$id);}
            $this->ajaxReturn(field_check('model',$where));
        }

    }
    //模型字段排序
    public function modelfield_edit(){
        $mid=I('get.mid','','int');
        if(!mid){return false;}
        $where['modelid']=$mid;

        if(IS_POST){
            $data=I('post.');
            foreach($data['listorders'] as $k=>$v){
                 $where['fieldid']=$k;
                 M('model_field')->where($where)->save(array('listorder'=>$v));
            }
            $data['status']='y';
            $this->ajaxReturn($data);
        }

        $list=M('model_field')->where($where)->order('listorder')->select();
        $this->assign('mid',$mid);
        $this->assign('list',$list);
        $this->display();
    }




}